package com.example.demospringboot;



import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.demospringboot.drwaing.entity.Player;
import com.example.demospringboot.daomapper.PlayerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.*;

@SpringBootTest
class test4 {
    // 每个部门的人数
    private static final int NUM_DEPT = 24;

    @Autowired
    private static PlayerMapper playerMapper;

    public static void main(String[] args) {
        int i = 0;
        while (i==0){
            i = b();
        }
    }
    public static int b() {
        try {

            // 创建总列表，包含所有部门人员
            List<Player> allPeople = new ArrayList<>();


            List<Player> otherPeople =  playerMapper.selectList(Wrappers.<Player>lambdaQuery().eq(true,Player::getTag,0));
            List<Player> harbin = playerMapper.selectList(Wrappers.<Player>lambdaQuery().eq(true,Player::getTag,1));
            List<Player> huhehaote =  playerMapper.selectList(Wrappers.<Player>lambdaQuery().eq(true,Player::getTag,2));
            List<Player> lanzhou =  playerMapper.selectList(Wrappers.<Player>lambdaQuery().eq(true,Player::getTag,3));
            List<Player> nanning =  playerMapper.selectList(Wrappers.<Player>lambdaQuery().eq(true,Player::getTag,4));
            List<Player> qingzang = playerMapper.selectList(Wrappers.<Player>lambdaQuery().eq(true,Player::getTag,5));


            allPeople.addAll(harbin);
            allPeople.addAll(huhehaote);
            allPeople.addAll(lanzhou);
            allPeople.addAll(nanning);
            allPeople.addAll(qingzang);
            allPeople.addAll(otherPeople);

            // 对所有人员进行随机打乱
            Collections.shuffle(allPeople);

            // 记录每个部门新的成员
            // 记录每个部门新的成员
            List<Player> harbinNew = new ArrayList<>();
            List<Player> huhehaoteNew = new ArrayList<>();
            List<Player> lanzhouNew = new ArrayList<>();
            List<Player> nanningNew = new ArrayList<>();
            List<Player> qingzangNew = new ArrayList<>();

            // 用于记录已经被分配的人员
            Set<Player> assignedPeople = new HashSet<>();

            // 按照要求分配每个部门24个人，并且确保不能在原部门
            assignPeople(harbin, harbinNew, allPeople,assignedPeople, NUM_DEPT);
            assignPeople(huhehaote, huhehaoteNew, allPeople,assignedPeople, NUM_DEPT);
            assignPeople(lanzhou, lanzhouNew, allPeople, assignedPeople,NUM_DEPT);
            assignPeople(nanning, nanningNew, allPeople, assignedPeople,NUM_DEPT);
            assignPeople(qingzang, qingzangNew, allPeople,assignedPeople, NUM_DEPT);
            // 输出结果
            printDepartment("哈尔滨局", harbinNew);
            printDepartment("呼和浩特局", huhehaoteNew);
            printDepartment("兰州局", lanzhouNew);
            printDepartment("南宁局", nanningNew);
            printDepartment("青藏公司", qingzangNew);
            return 1;
        }catch (Exception e){
            return 0;
        }

    }


    // 生成人员
    private static List<Player> generateharbin() {
        List<Player> department = new ArrayList<>();
        department.add(new Player(1,"哈尔滨局集团公司三间房站","内勤助理值班员","冯博文"));
        department.add(new Player(2,"哈尔滨局集团公司哈尔滨车务段","车站值班员","慕宇鸿"));
        department.add(new Player(3,"哈尔滨局集团公司海拉尔电务段","信号工","王琪"));
        department.add(new Player(101,"哈尔滨局集团公司海拉尔车务段","团委书记","王磊"));

        return department;
    }

    // 随机分配7个不在原部门的人员
    private static void assignPeople(List<Player> originalDepartment, List<Player> newDepartment, List<Player> allPeople, Set<Player> assignedPeople, int numDept) {
        List<Player> temp = new ArrayList<>(allPeople); // 创建副本避免修改原始列表
        temp.removeAll(originalDepartment); // 移除原部门人员

        // 过滤掉已经被分配过的人员
        temp.removeAll(assignedPeople);

        // 打乱剩余人员列表
        Collections.shuffle(temp);

        // 选取前numToAssign个未被分配且不在原部门的人员
        for (int i = 0; i < numDept; i++) {
            Player selectedPerson = temp.get(i);
            newDepartment.add(selectedPerson);
            assignedPeople.add(selectedPerson); // 标记该人员已经被分配
        }
    }

    // 输出每个部门的新成员
    private static void printDepartment(String departmentName, List<Player> department) {

        System.out.println(departmentName + " 新成员:");
        for (Player person : department) {
            System.out.println(person.getUnit()+person.getName());
        }
        System.out.println();
    }
}
